<?php

namespace app\controller;

use app\BaseController;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return View::fetch();
    }


    public function sm4CbcEncrypt()
    {
        $data = $this->request->post();
        return json(sm4_cbc_encrypt($data['msg'], $data['key']));
    }


    public function sm4CbcDecrypt()
    {

        $data = $this->request->post();
        return json(sm4_cbc_decrypt($data['msg'], $data['key']));
    }


    public function sm3Dgst()
    {
        $data = $this->request->post();
        return json(sm3_dgst($data['msg']));

    }


    public function sm2Sign()
    {
        $path = $this->request->post('path');
        $sign = $path . "_sign";
        sm2_sign($path, $sign);
        return json(['path' => $sign]);
    }


    public function sm2Verify()
    {
        $data = $this->request->post();
        return json(sm2_verify($data['sign_file_path'], $data['file_path']));
    }


    public function upload()
    {
        $file = $this->request->file('file');
        $savename = \think\facade\Filesystem::putFile('storage', $file);

        $path = "./public/" . $savename;
        return json(['path' => $path]);

    }


    public function download()
    {
        $path = $this->request->post('file_path');
        $realPath = upload_path() . ltrim($path, '.');
        return download($realPath, basename($path));
    }


    public function test()
    {
        $path = "./public/storage/20230426/27c4080c711de99c6078ecc4a3f1126f.txt_sign";
        var_dump(upload_path() . ltrim($path, '.'));
        var_dump(basename($path));
    }
}
